const baseURL = process.env.NODE_ENV === "development1" ?
"http://119.91.133.70":	//测试环境(请求接口的代理路径)http://10.20.28.192:8080
"https://www.yingyilife.com";//上线环境https://yingyilife.com

let HEADER = {
	"accept":"application/json",
	// "content-type": "application/x-www-form-urlencoded;charset=utf-8"
	"content-type": "application/json;charset=utf-8", //默认的
    "token": ''
}

//请求方法
export const request = (options) => {
    // 接口调用header设置token
    HEADER.token = getApp().globalData.token;
    console.log(HEADER)

	return new Promise((resolve,reject)=>{
        console.log(baseURL + options.url);
        console.log(options.data);

		uni.request({
			url: baseURL + options.url,
			header: HEADER,
			method: options.method || 'GET',
			data: options.data || {},
			success: (res)=>{
                console.log(res)
                const code = res.data.code
                
                if(code == 500){
					uni.showToast({
					    title: res.data.msg,
					    icon: 'none'
					})
					return reject(new Error(res.data.msg))
				} else if (code == 401) {
                    getApp().globalData.token = ''
                    getApp().globalData.nickName = ''
                    uni.setStorage({
                        key: 'user_login_key',
                        data: {}
                    })

                    // 未登录跳转登录页面
                    uni.showToast({
                        title: '请先登录',
                        icon: 'none'
                    })
                    setTimeout(function() {
                    	uni.switchTab({
                    	    url: '/pages/mine/index'
                    	})
                    }, 1000)

                    return reject('error')
                } else if(code != 200){
					uni.showToast({
						title: '数据获取失败',
						icon: 'none'
					})
					return reject('error')
				}

				resolve(res.data)
			},
			fail: (err)=>{
                console.log(err)
				uni.showToast({
					title: "网络开小差了~",
					icon:'none'
				})
				reject(err)
			}
		})
	})
}